package com.dragonfly.shutter.services;

import java.util.Date;

import com.dragonfly.shutter.DAOs.AlbumDAO;
import com.dragonfly.shutter.DAOs.BrowseLogDAO;
import com.dragonfly.shutter.DAOs.PictureAlbumDAO;
import com.dragonfly.shutter.DAOs.PictureDAO;
import com.dragonfly.shutter.beans.Album;
import com.dragonfly.shutter.beans.BrowseLog;
import com.dragonfly.shutter.beans.PictureAlbum;

/**
 * 设置相册封面服务类
 * @author 黄挺
 *
 */
public class SetAsEnvelopService {
	private AlbumDAO albumDAO;
	private BrowseLogDAO browseLogDAO;
	private PictureDAO pictureDAO;
	private PictureAlbumDAO pictureAlbumDAO;

	/**
	 * 设置相册服务函数
	 * @param pictureID 图片ID
	 * @param albumID 相册ID
	 * @param userID 用户ID
	 * @param requestUrl 请求URL
	 * @throws Exception 图片相册不匹配 
	 */
	public void doService(String pictureID, Integer albumID, 
			Integer userID, String requestUrl) throws Exception {
		//生成浏览信息
		BrowseLog browseLog = new BrowseLog();
		if(userID != null )
			browseLog.setUserID(userID);
		browseLog.setBrowseTime(new Date(System.currentTimeMillis()));
		browseLog.setUrl(requestUrl);
		
		
		doDBOperate(pictureID, albumID, browseLog);
	}
	
	/**
	 * 设置相册数据库操作
	 * @param pictureAlbum 图片相册关联Bean
	 * @param browseLog 日志Bean
	 * @throws Exception 图片相册不匹配
	 */
	public void doDBOperate(String pictureID, Integer albumID, BrowseLog browseLog) throws Exception{
		//图片不属于该相册
		PictureAlbum pictureAlbum = new PictureAlbum();
		pictureAlbum.setAlbumID(albumID);
		pictureAlbum.getPictureBrief().setPictureID(pictureID);
		
		if(pictureAlbumDAO.selectPictureAlbumByPK(pictureAlbum) == null ){
			throw new Exception("该图片不属于该相册");
		}
		
		browseLogDAO.insertBrowseLog(browseLog);
		albumDAO.updateEnvelopPicId(albumID, pictureID);
	};

	public void setAlbumDAO(AlbumDAO albumDAO) {
		this.albumDAO = albumDAO;
	}

	public void setBrowseLogDAO(BrowseLogDAO browseLogDAO) {
		this.browseLogDAO = browseLogDAO;
	}

	public void setPictureDAO(PictureDAO pictureDAO) {
		this.pictureDAO = pictureDAO;
	}

	public void setPictureAlbumDAO(PictureAlbumDAO pictureAlbumDAO) {
		this.pictureAlbumDAO = pictureAlbumDAO;
	}
}
